import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.net.ssl.SSLHandshakeException;

public class Server {
    public static void main(String[] args) {
        try {
            SSLServerSocketFactory sslserversocketfactory =
                    (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
            SSLServerSocket sslserversocket =
                    (SSLServerSocket) sslserversocketfactory.createServerSocket(9999);
            SSLSocket sslsocket = null;
	    while(true){
		try{
	    		sslsocket = (SSLSocket) sslserversocket.accept();
		}catch(SSLHandshakeException e){
			System.out.println(e);
			continue;
		}		

            	OutputStream os = sslsocket.getOutputStream();
            	OutputStreamWriter osw = new OutputStreamWriter(os);
	
	    	InputStream inputstream = sslsocket.getInputStream();
            	InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
            	BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

            	String msg = null;
            	while ((msg = bufferedreader.readLine()) != null) {
			msg = msg.toLowerCase();
                	System.out.println(msg);
               	 	System.out.flush();
			if(msg.contains("connection: keep-alive"))
				break;
            	}
	    	System.out.println("time to write");
	    	osw.write("Hello World!\n");    
	    	osw.close();         
       		} 
	}catch (Exception exception) {
        	exception.printStackTrace();
        }
    }
}
